<?php

$MENU_ARY = array(
1 => array('name'=>'product', 'pic1'=>'nav1.gif', 'pic2'=>'nav11.gif', 'url'=>'product.php'),
2 => array('name'=>'customer', 'pic1'=>'nav2.gif', 'pic2'=>'nav21.gif', 'url'=>'customer.php'),
3 => array('name'=>'media', 'pic1'=>'nav3.gif', 'pic2'=>'nav31.gif', 'url'=>'media.php'),
4 => array('name'=>'system', 'pic1'=>'nav4.gif', 'pic2'=>'nav41.gif', 'url'=>'system.php'),
5 => array('name'=>'my account', 'pic1'=>'nav5.gif', 'pic2'=>'nav51.gif', 'url'=>'myaccount.php'),
6 => array('name'=>'user', 'pic1'=>'nav6.gif', 'pic2'=>'nav61.gif', 'url'=>'user.php'),
);

//$excel_sql_ary = array(
//'Item#'=>'code',
//'Product name'=>'name',
//'Show'=>'hidden_flag',
//'New'=>'new_flag',
//'Remarks'=>'comment',
//'Big Pic'=>'pic',
//'Small Pic'=>'small_pic',
//'DCP'=>'price_2',
//'MCP'=>'price_1',
//'HKD'=>'price_3',
//'W [cm]'=>'width',
//'D [cm]'=>'length',
//'H [cm]'=>'height',
//'Cbm'=>'cbm',
//'W [in]'=>'width2',
//'D [in]'=>'length2',
//'H [in]'=>'height2',
//'Cuft'=>'cuft',
//'Finish Shown'=>'finishshown',
//'Fabric Shown'=>'fabricshown',
//'Leather Shown'=>'leathershown',
//'Marble Shown'=>'marbleshown',
//'Finish'=>'finish_id',
//'Setting'=>'setting_id',
//'Style / Period'=>'style_id',
//'Collection'=>'collection_id',
//'Story'=>'info'
//);

// 'Article description'=>'name',ʱ product name
//'Category Chinese'=>'Category_id', Category id  
//'Sub Category Chinese'=>'Sub_id',

$excel_sql_ary = array(
'Web code nr'=>'code',
'Article description'=>'name',
'Web Show'=>'hidden_flag',
'Web New'=>'new_flag',
'Web Remarks'=>'comment',
'Web Big Pic'=>'pic',
'Web Small Pic'=>'small_pic',
'Sales price'=>'price_2',
'wh Sales price'=>'price_1',
'HK Wholesale price'=>'price_3',
'L'=>'length',
'W'=>'width',
'H'=>'height',
'Volume'=>'cbm',
'W inches'=>'width2',
'D inches'=>'length2',
'H inches'=>'height2',
'Cuft'=>'cuft',
//'Finish Shown'=>'finishshown',
'Finish Shown'=>'finish_id',
'Fabric Shown'=>'fabricshown',
'Leather Shown'=>'leathershown',
'Marble Shown'=>'marbleshown',
//'Finishes'=>'finish_id',
'Finishes'=>'finishshown',
'Setting'=>'setting_id',
'Style Period'=>'style_id',
'Collection'=>'collection_id',
'Product information'=>'info',
//'Category'=>'Category_id',
'Sub Category'=>'sub_id',
//'Article description Chinese'=>'namezh',
//'Web Remarks Chinese'=>'commentzh',
//'Category Chinese'=>'Category_idzh',
/*'Sub Category Chinese'=>'Sub_idzh',
'Setting Chinese'=>'setting_idzh',
'Style Period Chinese'=>'style_idzh',
'Leather Shown Chinese'=>'leathershownzh',
'Marble Shown Chinese'=>'marbleshownzh',
'Product information Chinese'=>'infozh',*/
);


$excel_sql_ary_Insert = array(
//'Web code nr'=>'code',
'Article description'=>'name',
'L'=>'length',
'W'=>'width',
'H'=>'height',
'D inches'=>'length2',
'H inches'=>'height2',
'W inches'=>'width2',
'Volume'=>'cbm',
'Cuft'=>'cuft',
'Web Big Pic'=>'pic',
'Web Small Pic'=>'small_pic',
'wh Sales price'=>'price_1',
'Sales price'=>'price_2',
'HK Wholesale price'=>'price_3',
'Web New'=>'new_flag',
'Web Show'=>'hidden_flag',
'Web Remarks'=>'comment',
'Finishes'=>'finishshown',
//'Fabric Shown'=>'fabricshown',
//'Leather Shown'=>'leathershown',
'Marble Shown'=>'marbleshown',
'Product information'=>'info',
'Style Period'=>'style_id',
'Collection'=>'collection_id',
'Finish Shown'=>'finish_id',

/*'Setting'=>'setting_id',
 * 'Article description Chinese'=>'namezh',
'Web Remarks Chinese'=>'commentzh',
'Finishes Chinese'=>'finishshownzh',
'Fabric Shown Chinese'=>'fabricshownzh',
'Leather Shown Chinese'=>'leathershownzh',
'Marble Shown Chinese'=>'marbleshownzh',
'Product information Chinese'=>'infozh',*/
);




//EXCELĿ
$excel_category_column = array("Category"=>"category_id" , "Seq" => "seq");

// ûȨ
function checkAdmin() {
    global $_COOKIE, $admin_name;
    $admin_name = "";
    $AdminUser = GetCookie('AdminUser');

    if($AdminUser){
        $CK	= explode("\t",StrCode($AdminUser,'DECODE'));
        $admin_name = stripcslashes($CK[1]);
        if (checkpass($CK)) {
            return true;
        }
        return false;
    }
    return false;
}

function checkpass($CK){
    global $db;
    if (!$CK){
        return false;
    }

    $sql = "SELECT password FROM user WHERE username='$CK[1]' AND type = 10";
    //echo $sql;exit;
    $rt = $db->get_one($sql);
    if(!SafeCheck($CK,PwdCode($rt['password']))){
        return false;
    }

    return true;
}

function SafeCheck($CK,$PwdCode,$var='AdminUser',$expire=1800){
    global $timestamp;
    $t	= $timestamp - $CK[0];
    if($t > $expire || $CK[2] != md5($PwdCode.$CK[0])){
        Cookie($var,'',0);
        return false;
    }else{
        $CK[0] = $timestamp;
        $CK[2] = md5($PwdCode.$timestamp);
        $Value = implode("\t",$CK);
        $$var  = StrCode($Value);
        Cookie($var,StrCode($Value));
        return true;
    }
}

function PwdCode($pwd){
    return md5($_SERVER["HTTP_USER_AGENT"].$pwd.$GLOBALS['db_hash']);
}

function StrCode($string,$action='ENCODE'){
    $key	= substr(md5($_SERVER["HTTP_USER_AGENT"].$GLOBALS['db_hash']),8,18);
    $string	= $action == 'ENCODE' ? $string : base64_decode($string);
    $len	= strlen($key);
    $code	= '';
    for($i=0; $i<strlen($string); $i++){
        $k		= $i % $len;
        $code  .= $string[$i] ^ $key[$k];
    }
    $code = $action == 'DECODE' ? $code : base64_encode($code);
    return $code;
}

function getPassword($type=2) {
    //$USER_TYPE_ARY = array('1'=>'Wholesale Customer', '2'=>'Container Customer');   // 1=>w 2=>c
    $a = $type==2 ? "c" : "w";
    $tmp_ary = array('a','b','c','d','e','f','g','h','i','j','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z','2','3','4','5','6','7','8','9');
    shuffle($tmp_ary);
    return $a . $tmp_ary[1] . $tmp_ary[2] . $tmp_ary[3] . rand(1000,9999);
}

function goFromPage($msg = '') {
    global $PHP_SELF;
    $from_url = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $PHP_SELF;

    if (!empty($msg)) {
        if (strpos($from_url, "?")) {
            $from_url .= "&alert_message=" . urlencode($msg);
        } else {
            $from_url .= "?alert_message=" . urlencode($msg);
        }

    }


    if (!headers_sent($filename, $linenum)) {
        header("LOCATION:" . $from_url);
        exit;
    } else {
        //echo "Headers already sent in $filename on line $linenum\;
        echo "<script>location.href='" . $from_url . "';</script>";
        exit;
    }
}

function goEditPage($id) {
    global $PHP_SELF;
    $from_url = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $PHP_SELF;

    /*if (strstr($from_url, "id=")) {
        preg_replace('/id=(\d+)/', "id=$id", $from_url);
    } else*/
    if (strpos($from_url, "?")) {
        $from_url .= "&id=" . $id;
    } else {
        $from_url .= "?id=" . $id;
    }
    
    if (!headers_sent($filename, $linenum)) {
        header("LOCATION:" . $from_url);
        exit;
    } else {
        //echo "Headers already sent in $filename on line $linenum\;
        echo "<script>location.href='" . $from_url . "';</script>";
        exit;
    }
}
function goFromPagePro($msg = '',$arg='') {
    global $PHP_SELF;
    $from_url = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $PHP_SELF;

    if (!empty($msg)) {
        if (strpos($from_url, "?")) {
            $from_url .= "&alert_message=" . urlencode($msg);
        } else {
            $from_url .= "?alert_message=" . urlencode($msg);
        }

    }

    if (!headers_sent($filename, $linenum)) {
	    if(!empty($arg))
		$from_url.=$arg;
		
        header("LOCATION:" . $from_url);
        exit;
    } else {
        //echo "Headers already sent in $filename on line $linenum\;
        echo "<script>location.href='" . $from_url . "';</script>";
        exit;
    }
}

function GetProductCate($ary,$l,$arySelected=null,$arySeq =null)
{
	$s = '';
	$sp = '&nbsp;&nbsp;&nbsp;&nbsp;';
	for($i=0;$i<$l;$i++)
	{
		$sp .= '&nbsp;&nbsp;&nbsp;&nbsp;';	
	}
	$strSelected = '';
	$strTxtDisable = '';
	foreach($ary as $k => $v)
	{
		if($arySelected != null && in_array($k,$arySelected))
		{
			$strSelected = ' checked="checked"';
			$strTxtDisable = ' value="'.$arySeq[$k].'"';
		}
		else
		{
			$strSelected = '';	
			$strTxtDisable = 'disabled="disabled"';
		}
		$s.= '<tr class="tbcontent1">
				<td align="left">'.$sp.'<input type="checkbox" name="category_id[]" value="' . $v['id'] . '" onchange="if ( this.checked==true){seq_' . $v['id'] . '.disabled=false;} else {seq_' . $v['id'] . '.disabled=true;}" '.$strSelected.'/>' . $v['name'] . '</td>
				<td align="center"><input id="seq_' . $v['id'] . '" type="text" name="category_seq[' . $v['id'] . ']" size="3" '.$strTxtDisable.'/></td>
				</tr>';
		if(count($v["sub_cate"]) > 0)
		{			
			$s.=GetProductCate($v["sub_cate"],$l+1,$arySelected,$arySeq);
		}
	}	
	return $s;
}

// add by martin
function newGetProductCateIDAryByID($aryIn,$id,&$aryOut)
{
foreach($aryIn as $k=>$v)
	{
		if($v["id"] == $id)
		{
			newGetProductCateIDAry($v["sub_cate"],&$aryOut);
			$aryOut[$v["name"]] = $id;
		}	
	}

}
function newGetProductCateIDAry($aryIn,&$aryOut)
{
	foreach($aryIn as $k=>$v)
	{
		$aryOut[$v["name"]]=$v["id"];
		if(is_array($v["sub_cate"]) && count($v["sub_cate"])>0)
		{
			GetProductCateIDAry($v["sub_cate"],&$aryOut);	
		}
	}
}

//ݸIDΪKey,IDΪֵ
function GetProductCateIDAryByID($aryIn,$id,&$aryOut)
{
	foreach($aryIn as $k=>$v)
	{
		if($v["id"] == $id)
		{
			GetProductCateIDAry($v["sub_cate"],&$aryOut);
			$aryOut[$v["id"].' '.$v["name"]] = $id;
		}	
	}
}
//ΪKey,IDΪֵ
function GetProductCateIDAry($aryIn,&$aryOut)
{
	foreach($aryIn as $k=>$v)
	{
		$aryOut[$v["id"].' '.$v["name"]]=$v["id"];
		if(is_array($v["sub_cate"]) && count($v["sub_cate"])>0)
		{
			GetProductCateIDAry($v["sub_cate"],&$aryOut);	
		}
	}
}
?>